package com.hongxun.design.activitys;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.LogFillAdapter;
import com.hongxun.design.bean.LogBean;
import com.hongxun.design.bean.LogProjectBean;
import com.hongxun.design.bean.LogProjectItemBean;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.ui_listview.refreshlist.SwipeRefreshListView;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.PopwindowUtil;
import com.hongxun.design.utils.TimeUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/6/24.
 * 日志
 */
public class LogFillActivity extends BaseActivity implements HeadSecond, View.OnClickListener {
    TextView logfill_stime, logfill_mans, search_button;
    SwipeRefreshListView logfill_list;
    public TextView logfill_etime;
    /**
     * 时间选择变量*
     */
    public String startDate = "";
    public String endDate = "";
    String users = "";

    private Handler dataHandler;
    int startNum = 0;
    int addNum =  MyApp.addNum;
    List<LogBean> logBeans;
    List<LogProjectItemBean> adapterData;
    LogFillAdapter adapter;
    String edit="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logfill);
        findBar();
        findView();
    }

    @Override
    public void findBar() {
        HeadSecondControlPanel headsecond_layout = (HeadSecondControlPanel) findViewById(R.id.logfill_layout);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean(IconSourse.EDIT + "", View.VISIBLE, R.color.login_yellow, ListenerSet.back, "日志填报", R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
        headsecond_layout.mRightTitle.append(" 填报");
        headsecond_layout.setClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putString(MString.CODE,edit);
                IntentToActivity.intent(mActivity, LogEditActivity.class,bundle);
            }
        });

    }

    boolean isfirst=true;
    @Override
    protected void onResume() {
        if (!isfirst){
            Refresh();
        }
        isfirst=false;
        super.onResume();
    }


    @Override
    protected void findView() {
        super.findView();

        LinearLayout logfill_stime_button = (LinearLayout) findViewById(R.id.logfill_stime_button);
        LinearLayout logfill_etime_button = (LinearLayout) findViewById(R.id.logfill_etime_button);


        logfill_stime = (TextView) findViewById(R.id.logfill_stime);
        TextView logfill_stime_icon = (TextView) findViewById(R.id.logfill_stime_icon);


        logfill_etime = (TextView) findViewById(R.id.logfill_etime);
        TextView logfill_etime_icon = (TextView) findViewById(R.id.logfill_etime_icon);

        logfill_mans = (TextView) findViewById(R.id.logfill_mans);
        TextView logfill_mans_icon = (TextView) findViewById(R.id.logfill_mans_icon);

        search_button = (TextView) findViewById(R.id.logfill_search_button);

        logfill_list = (SwipeRefreshListView) findViewById(R.id.logfill_list);

        SetTypeface.set(logfill_stime_icon, IconSourse.Calendar);
        SetTypeface.set(logfill_etime_icon, IconSourse.Calendar);
        SetTypeface.set(logfill_mans_icon, IconSourse.DROPDOWN);
        SetTypeface.set(search_button, IconSourse.SEARCH);
        search_button.append(" 搜索");
        logfill_stime.setText("-");
        logfill_etime.setText("-");

        logfill_stime_button.setOnClickListener(this);
        logfill_etime_button.setOnClickListener(this);

        addHandler();
        inItList();
    }


    private void inItList() {
        logfill_list.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Refresh();
            }
        });
        Refresh();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.logfill_stime_button:
                ToastShow.showShort(this, "点击！");
                PopwindowUtil.openTimePicker(1970, logfill_stime, this, MString.startDate);
                break;

            case R.id.logfill_etime_button:
                if (startDate.length() == 0) {
                    ToastShow.showShort(this, "开始时间不能为空！");
                    return;
                }
                try {
                    String[] tar = startDate.split("-");
                    PopwindowUtil.openTimePicker(Integer.parseInt(tar[0]), logfill_etime, this, MString.endDate);
                } catch (Exception e) {
                }


                break;
        }
    }


    public void Search(View v) {
        Refresh();
    }


    private void Refresh() {
        startNum = 0;
        addNum =  MyApp.addNum;
        getHttp(HttpStringUtil.SUCCESSED);
    }

    private void getHttp(int code) {
        MyApp.HTTP.LogList(dataHandler, code, startDate, endDate, users, startNum, startNum + addNum);
    }


    private void addHandler() {
        dataHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (logBeans == null) {
                                logBeans = FastJsonUtil.JsonToGetLogList((String) msg.obj);
                            } else {
                                logBeans.clear();
                                logBeans.addAll(FastJsonUtil.JsonToGetLogList((String) msg.obj));
                            }
                            startNum = startNum + addNum;
                            dataHandler.sendEmptyMessage(1);
                        } else {
                            ToastShow.showShort(mActivity, FastJsonUtil.JsonToCheck_String((String) msg.obj));
                            logfill_list.setRefreshing(false);
                        }
                        break;

                    case 1:

                        List<LogProjectItemBean> data = new ArrayList<>();
                        int NUM;
                        for (LogBean l : logBeans) {
                            NUM = 0;
                            if (l.getProject().size() != 0) {
                                for (LogProjectBean p : l.getProject()) {
                                    data.add(new LogProjectItemBean(NUM, l, p));
                                    NUM++;
                                }
                                data.add(new LogProjectItemBean(NUM, l, null));
                            } else {
                                data.add(new LogProjectItemBean(NUM, l, null));
                            }
                        }
                        if (adapterData == null) adapterData = new ArrayList<>();
                        adapterData.clear();
                        adapterData.addAll(data);
                        if (adapter == null) {
                            adapter = new LogFillAdapter(mActivity, adapterData);
                            logfill_list.setAdapter(adapter);
                            logfill_list.setFragment(baseActivity);
                            logfill_list.setCanScroll(true);
                        } else {
                            logfill_list.update();
                            logfill_list.setRefreshing(false);
                            logfill_list.removeFooterView();
                        }
                        try {
                            if (logBeans.size()>0){
                                if (logBeans.get(0).getCreate_time().contains(TimeUtil.getTime())){
                                    edit= JSON.toJSONString(logBeans.get(0));
                                }
                                MyLog.L(MString.SUCCESSED+" 日志："+edit);
                            }
                        }catch (Exception e){}
                        break;
                    case HttpStringUtil.ADDMROE:
                        int num = 0;
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (logBeans == null) {
                                logBeans = FastJsonUtil.JsonToGetLogList((String) msg.obj);
                            } else {
                                num = Integer.parseInt(logBeans.size() + "");
                                logBeans.addAll(FastJsonUtil.JsonToGetLogList((String) msg.obj));
                            }
                            dataHandler.sendEmptyMessage(1);
                            if (num < logBeans.size()) {
                                startNum = startNum + addNum;
                            } else {
                                ToastShow.showShort(mActivity, "没有数据了");
                            }
                        } else {
                            ToastShow.showShort(mActivity, FastJsonUtil.JsonToCheck_String((String) msg.obj));
                            logfill_list.setRefreshing(false);
                        }

                        break;
                    case HttpStringUtil.ERROR:
                        logfill_list.setRefreshing(false);
                        break;
                }
            }
        };
    }

    @Override
    public void addMore() {
        super.addMore();
        getHttp(HttpStringUtil.ADDMROE);
    }

    @Override
    public void onDestroy() {

        logfill_list.destory(logfill_list);
        super.onDestroy();
    }
}
